package com.ld.security.auth.client.runner;

import com.ld.security.auth.common.feign.AuthClientFeign;
import com.ld.security.common.shiro.util.SessionUtil;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Log
@Component
@Order(1)
public class AuthClientRunner implements CommandLineRunner {

    @Autowired
    private AuthClientFeign authFeign;


    @Autowired
    private LoadBalancerClient loadBalancer;
    @Autowired
    private DiscoveryClient discoveryClient;

    int manLogin =10 ;

    int nowLogin = 0;


    @Value("${spring.application.name}")
    private String appName ;

//   public void login(){
//
//
//       if(AuthUtil.getThisServerToken() != null){
//           return;
//       }else if(nowLogin >= manLogin){
//           Console.log("尝试登录次数异常,取消登录");
//           return;
//       }
//       FeignClient feignClient = AnnotationUtils.findAnnotation( authFeign.getClass(),FeignClient.class);
//       ServiceInstance serviceInstance = loadBalancer.choose(feignClient.value());
//
//       if(serviceInstance == null){
//           Console.log("认证服务挂了,等待下次");
//           return;
//       }
//       LoginPo po =   new LoginPo();
//       po.setPassword("ldl");
//       po.setUsername("ldl");
//       try {
//           DataVo<TokenVo> vo = authFeign.login(po);
//           vo.throwEx();
//           TokenVo tokenVo =  vo.getData();
//
//           AuthUtil.setThisServerToken(tokenVo.getToken());
//           AuthUtil.setServerToken(tokenVo.getToken());
//       }catch (Exception e){
//           e.printStackTrace();
//           nowLogin++;
//       }
//   }





    @Override
    public void run(String... args) throws Exception {
        SessionUtil.setAppName(appName);

    }
}
